package com.wthink.card.Thread;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.wthink.card.ShdThread;
import com.wthink.card.models.ShdFrameBundle;
import com.wthink.card.models.Utils;

import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class PersecProcThread extends ShdThread {

    private Handler handler;
    SocketFrameProcThread socketFrameProcThread;
    SerialFrameProcThread serialFrameProcThread;

    String msg = "SENDDATA";
    ByteBuffer frameBuf = ByteBuffer.allocate(250);
    public PersecProcThread(Context ctx,SocketFrameProcThread socketFrameProcThread,SerialFrameProcThread serialFrameProcThread,Handler handler){
        super(ctx, "");
        this.handler = handler;
        this.serialFrameProcThread = serialFrameProcThread;
        this.socketFrameProcThread = socketFrameProcThread;

    }

    public void sendMsgToHandler(int what) {
        handler.sendMessage(handler.obtainMessage(what));
    }

    public void run() {
        try{
            int i = 0;
            while(!isInterrupted())
            {
                if(i % 3 == 0){
                    //心跳包
                    //操作屏升级状态下不发送
                    byte []	data = msg.toString().getBytes(Charset.forName("GB2312"));
                    //ByteBuffer frameBuf = ByteBuffer.allocate(16);
                    //frameBuf.put(msg.getBytes(Charset.forName("GB2312")));

                    if(serialFrameProcThread != null && SerialFrameProcThread.GetScreenWorkMode()==SerialFrameProcThread.SCREEN_WORK_MODE_NORMAL){
                        //serialFrameProcThread.SendToMainBoardNormalFrm(ShdFrameBundle.CFS_KPLINE, frameBuf.array(), frameBuf.position());
                        //serialFrameThread.SendToMainBoardNormalFrm(0x01, data, data.length);
                    }
                }
                //喂狗
                if(i % 10 == 0) {
                    if(socketFrameProcThread != null){
                        frameBuf.clear();
                        frameBuf.put((byte) 7);
                        frameBuf.put((byte) 120);
                        if(socketFrameProcThread != null)
                            socketFrameProcThread.SendToMainBoardNormalFrm(ShdFrameBundle.B_SOCKET_FRAME_01, frameBuf.array(), 2, ShdFrameBundle.A_SOCKET_MODEL_01);
                    }
                }
                i++;
                Thread.sleep(Utils.OS_TICKS_PER_SEC);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
